/**
 * @overview 创建覆盖物
 * @author dengqiang
 * @description 鲸云CgsOverlay
 * @update  2022.03.21
 */
class GOverlay {
  constructor() {
    window.GOverlayArray = []
  }

  //新增单个覆盖物
  add(paramObj) {
    const { name, parentName, option } = paramObj
    const overlay = new CgsOverlay({
      position: option.position,
      positioning: option.positioning || 'bottom-right', //覆盖物相对于坐标覆盖物的方位
      element: option.elementName,
      offset: option.offset || [-20, -30], //偏移
      draggable: option.draggable || false,
      showLine: option.showLine || false,
      brokenLine: false,
      overlay3DIndex: 1,
      lineStyle: {
        width: 2,
        color: 'rgba(0,249,249,0.7)',
      },
    })

    overlay.attribute = option.attribute //支持添加自定义属性
    const overlayObject = {
      name: name,
      parentName: parentName,
      overlay: overlay,
    }
    window.GOverlayArray.push(overlayObject)
    window.map.addOverlay(overlay)
    return overlay
  }

  //根据name移除单个覆盖物
  removeByName(name) {
    window.GOverlayArray.forEach((item, index) => {
      if (item.name == name) {
        window.map.removeOverlay(item.overlay)
        window.GOverlayArray.splice(index, 1)
      }
    })
  }

  //根据parentName移除一批覆盖物
  removeByPatName(parentName) {
    const length = window.GOverlayArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GOverlayArray[i].parentName == parentName) {
        window.map.removeOverlay(window.GOverlayArray[i].overlay)
        window.GOverlayArray.splice(i, 1)
      }
    }
  }

  //移除所有覆盖物
  removeAll() {
    window.GOverlayArray.forEach((item) => {
      window.map.removeOverlay(item.overlay)
    })
    window.GOverlayArray = []
  }

  //根据name控制单个覆盖物的显隐
  setVisibleByName(name, boolean) {
    window.GOverlayArray.forEach((item) => {
      if (item.name == name) {
        item.overlay.setVisible(boolean)
      }
    })
  }
  //根据name控制单个覆盖物的显隐
  setPositionByName(name, pos) {
    window.GOverlayArray.forEach((item) => {
      if (item.name == name) {
        item.overlay.setPosition(pos)
      }
    })
  }

  //根据parentName控制一批覆盖物的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GOverlayArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.overlay.setVisible(boolean)
      }
    })
  }
}

export default GOverlay
